/*
package com.jyy.prefabricated.service.impl;

import com.alibaba.excel.EasyExcel;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.StudentAchievementDto;
import com.jyy.prefabricated.dto.StudentDto;
import com.jyy.prefabricated.dto.StudentSignUpApplyDto;
import com.jyy.prefabricated.entity.*;
import com.jyy.prefabricated.enums.*;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.listener.AchievementExcelListener;
import com.jyy.prefabricated.listener.EnrollsExcelListener;
import com.jyy.prefabricated.mapper.*;
import com.jyy.prefabricated.service.ProjectCategoryService;
import com.jyy.prefabricated.service.ProjectInfoService;
import com.jyy.prefabricated.service.StudentAchievementService;
import com.jyy.prefabricated.vo.AchievementData;
import com.jyy.prefabricated.vo.EnrollsProjectData;
import com.jyy.prefabricated.vo.StudentExamPaperVo;
import com.jyy.prefabricated.vo.StudentSignUpApplyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

*/
/**
 * 学生成绩表(StudentAchievement)表服务实现类
 *
 * @author mrl
 * @since 2021-07-07 16:19:14
 *//*

@Service("studentAchievementService")
public class StudentAchievementServiceImpl implements StudentAchievementService {
    @Resource
    private StudentAchievementDao studentAchievementDao;
    @Resource
    private ProjectCategoryMapper projectCategoryMapper;
    @Resource
    private StudentSignUpApplyMapper studentSignUpApplyMapper;
    @Resource
    private StudentExamMapper studentExamMapper;
    @Resource
    private EligibilityRulesDao eligibilityRulesDao;
    @Resource
    private ProjectInfoService projectInfoService;
    @Resource
    private ProjectCategoryService projectCategoryService;
    */
/**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     *//*

    @Override
    public StudentAchievement queryById(Integer id) {
        return this.studentAchievementDao.queryById(id);
    }

    @Override
    public StudentAchievement queryByCategoryIdId(Integer categoryId, Integer studentId) {
        StudentAchievement studentAchievement = new StudentAchievement();
        StudentSignUpApplyDto studentSignUpApplyDto = new StudentSignUpApplyDto();
        studentSignUpApplyDto.setCategoryId(categoryId);
        studentSignUpApplyDto.setStudentId(studentId);
        List<StudentSignUpApplyVo> studentSignUpApplyVos = studentSignUpApplyMapper.selectByParam(studentSignUpApplyDto);
        if(studentSignUpApplyVos != null && studentSignUpApplyVos.size() > 0 ){
            studentAchievement.setName(studentSignUpApplyVos.get(0).getName());
            studentAchievement.setCategoryId(categoryId);
            studentAchievement.setCategoryName(studentSignUpApplyVos.get(0).getClassifyName());
            studentAchievement.setIdCard(studentSignUpApplyVos.get(0).getIdCard());
            studentAchievement.setStudentId(studentId);
            studentAchievement.setSex(studentSignUpApplyVos.get(0).getSex());
        }
        StudentExamPaperVo studentExamPaperVo = studentExamMapper.selectByStudentCategoryId(categoryId,studentId);
        if(studentExamPaperVo != null){
            studentAchievement.setGradeOne(studentExamPaperVo.getTotalScore());
        }
        int fId = projectCategoryService.findCategoryIdById(categoryId);
        studentAchievement.setFId(fId);
        StudentAchievementDto student = new StudentAchievementDto();
        student.setCategoryId(fId);
        student.setStudentId(studentId);
        StudentAchievement maxAchievement = studentAchievementDao.selectMaxAchievement(student);
        if(maxAchievement != null){
            studentAchievement.setGradeTwo(maxAchievement.getGradeTwo());
        }
        EligibilityRules eligibilityRules = new EligibilityRules();
        eligibilityRules.setCategoryId(categoryId);
        List<EligibilityRules> eligibilityRulesList = eligibilityRulesDao.queryAll(eligibilityRules);
        if(eligibilityRulesList != null && eligibilityRulesList.size() > 0 ){
            int qualifiedCount = 0;
            for (EligibilityRules eligibilityRule:eligibilityRulesList) {
                boolean qualified = false;
                if(eligibilityRule.getGradeOne()!=null && eligibilityRule.getGradeTwo()!=null){
                    if(studentAchievement.getGradeOne() != null && studentAchievement.getGradeTwo() != null){
                        if(studentAchievement.getGradeOne() > eligibilityRule.getGradeOne() && studentAchievement.getGradeTwo().equals(eligibilityRule.getGradeTwo())){
                            qualified = true;
                        }
                    }

                }else if(eligibilityRule.getGradeOne()!=null && eligibilityRule.getGradeTwo()==null){
                    if(studentAchievement.getGradeOne() != null && studentAchievement.getGradeOne() > eligibilityRule.getGradeOne()){
                        qualified = true;
                    }
                }else if(eligibilityRule.getGradeOne()==null && eligibilityRule.getGradeTwo()!=null){
                    if(studentAchievement.getGradeTwo() != null && studentAchievement.getGradeTwo().equals(eligibilityRule.getGradeTwo())){
                        qualified = true;
                    }
                }
                if(qualified){
                    qualifiedCount++;
                }
            }
            if(qualifiedCount>0){
                studentAchievement.setQualified(1);
            }else{
                studentAchievement.setQualified(0);
            }
        }
        return studentAchievement;
    }

    */
/**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     *//*

    @Override
    public List<StudentAchievement> queryAllByLimit(int offset, int limit) {
        return this.studentAchievementDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<StudentAchievement> selectByParams(StudentAchievementDto studentAchievementDto) {
        StudentAchievement studentAchievement = new StudentAchievement();
        BeanUtils.copyProperties(studentAchievementDto, studentAchievement);
        List<StudentAchievement> studentAchievements = studentAchievementDao.queryAll(studentAchievement);
        return studentAchievements;
    }

    @Override
    public List<StudentAchievement> selectHistoryAchievement(StudentAchievementDto studentAchievementDto) {
        StudentAchievement studentAchievement = new StudentAchievement();
        BeanUtils.copyProperties(studentAchievementDto, studentAchievement);
        List<StudentAchievement> result = new ArrayList<>();
        int fId = projectCategoryService.findCategoryIdById(studentAchievementDto.getCategoryId());
        StudentAchievement student = new StudentAchievement();
        student.setCategoryId(fId);
        student.setStudentId(studentAchievementDto.getStudentId());
        List<StudentAchievement> studentAchievements = studentAchievementDao.queryAll(studentAchievement);
        List<StudentExamPaperVo>  studentExamPaperVos = studentExamMapper.selectHistoryAchievement(studentAchievementDto.getCategoryId(),studentAchievementDto.getStudentId());
        if(studentAchievements != null && studentAchievements.size() >0 && studentExamPaperVos != null && studentExamPaperVos.size() >0 ){
            if(studentAchievements.size() >= studentExamPaperVos.size()){
                for (int i = 0; i<studentExamPaperVos.size(); i++) {
                    studentAchievements.get(i).setGradeOne(studentExamPaperVos.get(i).getTotalScore());
                }
                result.addAll(studentAchievements);
            } else{
                for (StudentExamPaperVo studentExamPaperVo:studentExamPaperVos) {
                    StudentAchievement achievement = new StudentAchievement();
                    achievement.setGradeOne(studentExamPaperVo.getTotalScore());
                    result.add(achievement);
                }
                for (int i = 0; i<studentAchievements.size(); i++) {
                    result.get(i).setGradeTwo(studentAchievements.get(i).getGradeTwo());
                }
            }
        } else if(studentAchievements != null && studentAchievements.size() >0 && studentExamPaperVos.isEmpty()){
            result.addAll(studentAchievements);
        } else if(studentAchievements.isEmpty() && studentExamPaperVos != null && studentExamPaperVos.size() >0 ){
            for (StudentExamPaperVo studentExamPaperVo:studentExamPaperVos) {
                StudentAchievement achievement = new StudentAchievement();
                achievement.setGradeOne(studentExamPaperVo.getTotalScore());
                result.add(achievement);
            }
        }
        return result;
    }

    @Override
    public List<StudentAchievement> selectMyAchievement(StudentAchievementDto studentAchievementDto) {
        List<StudentAchievement> result = new ArrayList<>();
        StudentSignUpApply studentSignUpApply = new StudentSignUpApply();
        studentSignUpApply.setTrainingOrganizationId(studentAchievementDto.getBuyId());
        if(studentAchievementDto.getName() !=null && !"".equals(studentAchievementDto.getName())){
            studentSignUpApply.setName(studentAchievementDto.getName());
        }

        if(studentAchievementDto.getIdCard() !=null && !"".equals(studentAchievementDto.getIdCard())) {
            studentSignUpApply.setIdCard(studentAchievementDto.getIdCard());
        }
        List<StudentSignUpApplyVo> studentSignUpApplyList = studentSignUpApplyMapper.selectByBuyer(studentSignUpApply);
        if(studentSignUpApplyList != null && studentSignUpApplyList.size() > 0 ){
            for (StudentSignUpApplyVo signUpApply:studentSignUpApplyList) {
                StudentAchievement studentAchievement = new StudentAchievement();
                StudentSignUpApplyDto studentSignUpApplyDto = new StudentSignUpApplyDto();
                studentSignUpApplyDto.setCategoryId(signUpApply.getClassifyId());
                studentSignUpApplyDto.setStudentId(signUpApply.getStudentId());
                studentSignUpApplyDto.setStatus(5);
                studentSignUpApplyDto.setPayNo(1);
                List<StudentSignUpApplyVo> studentSignUpApplyVos = studentSignUpApplyMapper.selectByParam(studentSignUpApplyDto);
                if(studentSignUpApplyVos != null && studentSignUpApplyVos.size() > 0 ){
                    studentAchievement.setName(studentSignUpApplyVos.get(0).getName());
                    studentAchievement.setCategoryId(signUpApply.getClassifyId());
                    studentAchievement.setCategoryName(studentSignUpApplyVos.get(0).getClassifyName());
                    studentAchievement.setIdCard(studentSignUpApplyVos.get(0).getIdCard());
                    studentAchievement.setStudentId(signUpApply.getStudentId());
                }
                StudentExamPaperVo studentExamPaperVo = studentExamMapper.selectByStudentCategoryId(signUpApply.getClassifyId(),signUpApply.getStudentId());
                if(studentExamPaperVo != null){
                    studentAchievement.setGradeOne(studentExamPaperVo.getTotalScore());
                }
                int fId = projectCategoryService.findCategoryIdById(signUpApply.getClassifyId());
                StudentAchievementDto student = new StudentAchievementDto();
                student.setCategoryId(fId);
                student.setStudentId(signUpApply.getStudentId());
                StudentAchievement maxAchievement = studentAchievementDao.selectMaxAchievement(student);
                if(maxAchievement != null){
                    studentAchievement.setGradeTwo(maxAchievement.getGradeTwo());
                }
                EligibilityRules eligibilityRules = new EligibilityRules();
                eligibilityRules.setCategoryId(signUpApply.getClassifyId());
                List<EligibilityRules> eligibilityRulesList = eligibilityRulesDao.queryAll(eligibilityRules);
                if(eligibilityRulesList != null && eligibilityRulesList.size() > 0 ){
                    int qualifiedCount = 0;
                    for (EligibilityRules eligibilityRule:eligibilityRulesList) {
                        boolean qualified = false;
                        if(eligibilityRule.getGradeOne()!=null && eligibilityRule.getGradeTwo()!=null){
                            if(studentAchievement.getGradeOne() != null && studentAchievement.getGradeTwo() != null){
                                if(studentAchievement.getGradeOne() > eligibilityRule.getGradeOne() && studentAchievement.getGradeTwo().equals(eligibilityRule.getGradeTwo())){
                                    qualified = true;
                                }
                            }

                        }else if(eligibilityRule.getGradeOne()!=null && eligibilityRule.getGradeTwo()==null){
                            if(studentAchievement.getGradeOne() != null && studentAchievement.getGradeOne() > eligibilityRule.getGradeOne()){
                                qualified = true;
                            }
                        }else if(eligibilityRule.getGradeOne()==null && eligibilityRule.getGradeTwo()!=null){
                            if(studentAchievement.getGradeTwo() != null && studentAchievement.getGradeTwo().equals(eligibilityRule.getGradeTwo())){
                                qualified = true;
                            }
                        }
                        if(qualified){
                            qualifiedCount++;
                        }
                    }
                    if(qualifiedCount>0){
                        studentAchievement.setQualified(1);
                    }else{
                        studentAchievement.setQualified(0);
                    }
                    if(studentAchievementDto.getGradeTwo() != null){
                        if(studentAchievementDto.getGradeTwo().equals(studentAchievement.getGradeTwo())){
                            result.add(studentAchievement);
                        }
                    }
                    if(studentAchievementDto.getQualified() != null){
                        if(studentAchievementDto.getQualified().equals(studentAchievement.getQualified())){
                            result.add(studentAchievement);
                        }
                    }
                    if(studentAchievementDto.getGradeTwo() == null && studentAchievementDto.getQualified() == null){
                        result.add(studentAchievement);
                    }

                }
            }

        }

        */
/*List<StudentAchievement> studentAchievements = studentAchievementDao.selectMyAchievement(studentAchievementDto);*//*

        return result;
    }

    */
/**
     * 新增数据
     *
     * @param studentAchievement 实例对象
     * @return 实例对象
     *//*

    @Override
    public StudentAchievement insert(StudentAchievement studentAchievement) {
        this.studentAchievementDao.insert(studentAchievement);
        return studentAchievement;
    }

    @Override
    public AjaxResult importStudentAchieve(MultipartFile file) {

        try {
            List<StudentAchievement> studentAchievements = new ArrayList<StudentAchievement>();

            AchievementExcelListener listener = new AchievementExcelListener();

            EasyExcel.read(file.getInputStream(), AchievementData.class, listener).sheet().doRead();
            studentAchievements = listener.getStudentAchievements();
            if (studentAchievements.isEmpty()) {
                //throw new CustomException("导入学员信息为空");
                return AjaxResult.error(-1,"导入学员信息为空");
            }
            if (studentAchievements.size() > 0) {
                for (StudentAchievement studentAchievement : studentAchievements) {
                    List<ProjectCategory> projectCategories = projectCategoryMapper.selectByParams(studentAchievement.getCategoryName(), null);
                    if(projectCategories != null && projectCategories.size()>0){
                        studentAchievement.setCategoryId(projectCategories.get(0).getId());
                        StudentSignUpApplyDto studentSignUpApplyDto = new StudentSignUpApplyDto();
                        */
/*studentSignUpApplyDto.setCategoryId(studentAchievement.getCategoryId());*//*

                        studentSignUpApplyDto.setIdCard(studentAchievement.getIdCard());
                        List<Integer> categoryIds = projectInfoService.selectDeepCategoryIds(studentAchievement.getCategoryId());
                        studentSignUpApplyDto.setCategoryIds(categoryIds);
                        List<StudentSignUpApplyVo> studentSignUpApplyVos = studentSignUpApplyMapper.selectByParam(studentSignUpApplyDto);
                        if(studentSignUpApplyVos != null && studentSignUpApplyVos.size()>0){
                            studentAchievement.setName(studentSignUpApplyVos.get(0).getName());
                            studentAchievement.setSex(studentSignUpApplyVos.get(0).getSex());
                            studentAchievement.setStudentId(studentSignUpApplyVos.get(0).getStudentId());
                            studentAchievement.setCreateTime(new Date());
                            */
/*if(studentAchievement.getCategoryId() != null && studentAchievement.getStudentId() != null){
                                StudentExamPaperVo studentExamPaperVo = studentExamMapper.selectByStudentCategoryId(studentAchievement.getCategoryId(),studentAchievement.getStudentId());
                                studentAchievement.setGradeOne(studentExamPaperVo.getTotalScore());
                                EligibilityRules eligibilityRules = new EligibilityRules();
                                eligibilityRules.setCategoryId(studentAchievement.getCategoryId());
                                if(studentAchievement.getGradeOne() != null && studentAchievement.getGradeTwo() != null){
                                    List<EligibilityRules> eligibilityRulesList = eligibilityRulesDao.queryAll(eligibilityRules);
                                    if(eligibilityRulesList != null && eligibilityRulesList.size() > 0 ){
                                        int qualifiedCount = 0;
                                        for (EligibilityRules eligibilityRule:eligibilityRulesList) {
                                            boolean qualified = false;
                                            if(eligibilityRule.getGradeOne()!=null && eligibilityRule.getGradeTwo()!=null){
                                                if(studentAchievement.getGradeOne() > eligibilityRule.getGradeOne() && studentAchievement.getGradeTwo().equals(eligibilityRule.getGradeTwo())){
                                                    qualified = true;
                                                }
                                            }else if(eligibilityRule.getGradeOne()!=null && eligibilityRule.getGradeTwo()==null){
                                                if(studentAchievement.getGradeOne() > eligibilityRule.getGradeOne()){
                                                    qualified = true;
                                                }
                                            }else if(eligibilityRule.getGradeOne()==null && eligibilityRule.getGradeTwo()!=null){
                                                if(studentAchievement.getGradeTwo().equals(eligibilityRule.getGradeTwo())){
                                                    qualified = true;
                                                }
                                            }
                                            if(qualified){
                                                qualifiedCount++;
                                            }
                                        }
                                        if(qualifiedCount>0){
                                            studentAchievement.setQualified(1);
                                        }else{
                                            studentAchievement.setQualified(0);
                                        }
                                        studentAchievement.setCreateTime(new Date());
                                    }
                                }

                            }*//*


                        }
                    }

                }
                studentAchievementDao.insertBatch(studentAchievements);
            }

        } catch (Exception e) {
            throw new CustomException("批量导入失败");
        }
        return AjaxResult.success();
    }

    */
/**
     * 修改数据
     *
     * @param studentAchievementDto 实例对象
     * @return 实例对象
     *//*

    @Override
    public int update(StudentAchievementDto studentAchievementDto) {
        StudentAchievement studentAchievement = new StudentAchievement();
        BeanUtils.copyProperties(studentAchievementDto, studentAchievement);
        StudentAchievement achievement = this.queryById(studentAchievement.getId());
        if(achievement.getGradeOne() != null && studentAchievementDto.getGradeTwo() != null){
            EligibilityRules eligibilityRules = new EligibilityRules();
            eligibilityRules.setCategoryId(achievement.getCategoryId());
            List<EligibilityRules> eligibilityRulesList = eligibilityRulesDao.queryAll(eligibilityRules);
            if(eligibilityRulesList != null && eligibilityRulesList.size() > 0 ) {
                int qualifiedCount = 0;
                for (EligibilityRules eligibilityRule : eligibilityRulesList) {
                    boolean qualified = false;
                    if (eligibilityRule.getGradeOne() != null && eligibilityRule.getGradeTwo() != null) {
                        if (achievement.getGradeOne() > eligibilityRule.getGradeOne() && studentAchievementDto.getGradeTwo().equals(eligibilityRule.getGradeTwo())) {
                            qualified = true;
                        }
                    } else if (eligibilityRule.getGradeOne() != null && eligibilityRule.getGradeTwo() == null) {
                        if (achievement.getGradeOne() > eligibilityRule.getGradeOne()) {
                            qualified = true;
                        }
                    } else if (eligibilityRule.getGradeOne() == null && eligibilityRule.getGradeTwo() != null) {
                        if (studentAchievementDto.getGradeTwo().equals(eligibilityRule.getGradeTwo())) {
                            qualified = true;
                        }
                    }
                    if (qualified) {
                        qualifiedCount++;
                    }
                }
                if (qualifiedCount > 0) {
                    studentAchievement.setQualified(1);
                } else {
                    studentAchievement.setQualified(0);
                }
                studentAchievement.setUpdateTime(new Date());
            }
        }


        return this.studentAchievementDao.update(studentAchievement);
    }

    @Override
    public int updateAchieve(StudentAchievementDto studentAchievementDto) {
        StudentAchievement studentAchievement = new StudentAchievement();
        studentAchievement.setCategoryId(studentAchievementDto.getCategoryId());
        studentAchievement.setStudentId(studentAchievementDto.getStudentId());
        int result = 0;
        List<StudentAchievement> studentAchievements = studentAchievementDao.queryAll(studentAchievement);
        if(studentAchievements != null && studentAchievements.size() > 0){
            studentAchievement.setGradeTwo(studentAchievementDto.getGradeTwo());
            result = studentAchievementDao.updateByCategory(studentAchievement);
        }else{
            result = -1;
        }

        return result;
    }

    */
/**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     *//*

    @Override
    public boolean deleteById(Integer id) {
        return this.studentAchievementDao.deleteById(id) > 0;
    }

    @Override
    public boolean combinedResults() {
        StudentAchievement studentAchievement = new StudentAchievement();
        List<StudentAchievement> studentAchievements = studentAchievementDao.queryAll(studentAchievement);
        List<StudentAchievement> updateStudentAchievements = new ArrayList<>();
        for (StudentAchievement achievement:studentAchievements) {
                if(achievement.getCategoryId() != null && achievement.getStudentId() != null){
                    StudentExamPaperVo studentExamPaperVo = studentExamMapper.selectByStudentCategoryId(achievement.getCategoryId(),achievement.getStudentId());

                    if(achievement.getGradeOne() != null && achievement.getGradeTwo() != null){
                        if(studentExamPaperVo.getTotalScore() != null && studentExamPaperVo.getTotalScore() > achievement.getGradeOne()){
                            StudentAchievement updateStudentAchievement = new StudentAchievement();
                            updateStudentAchievement.setId(achievement.getId());
                            updateStudentAchievement.setGradeOne(studentExamPaperVo.getTotalScore());
                            EligibilityRules eligibilityRules = new EligibilityRules();
                            eligibilityRules.setCategoryId(achievement.getCategoryId());
                            List<EligibilityRules> eligibilityRulesList = eligibilityRulesDao.queryAll(eligibilityRules);
                            if(eligibilityRulesList != null && eligibilityRulesList.size() > 0 ){
                                int qualifiedCount = 0;
                                for (EligibilityRules eligibilityRule:eligibilityRulesList) {
                                    boolean qualified = false;
                                    if(eligibilityRule.getGradeOne()!=null && eligibilityRule.getGradeTwo()!=null){
                                        if(updateStudentAchievement.getGradeOne() > eligibilityRule.getGradeOne() && achievement.getGradeTwo().equals(eligibilityRule.getGradeTwo())){
                                            qualified = true;
                                        }
                                    }else if(eligibilityRule.getGradeOne()!=null && eligibilityRule.getGradeTwo()==null){
                                        if(updateStudentAchievement.getGradeOne() > eligibilityRule.getGradeOne()){
                                            qualified = true;
                                        }
                                    }else if(eligibilityRule.getGradeOne()==null && eligibilityRule.getGradeTwo()!=null){
                                        if(achievement.getGradeTwo().equals(eligibilityRule.getGradeTwo())){
                                            qualified = true;
                                        }
                                    }
                                    if(qualified){
                                        qualifiedCount++;
                                    }
                                }
                                if(qualifiedCount>0){
                                    updateStudentAchievement.setQualified(1);
                                }else{
                                    updateStudentAchievement.setQualified(0);
                                }
                                updateStudentAchievement.setUpdateTime(new Date());
                                updateStudentAchievements.add(updateStudentAchievement);
                            }
                        }

                    }

                }
        }
        if(updateStudentAchievements != null && updateStudentAchievements.size()>0){
            int count = studentAchievementDao.updateBatch(updateStudentAchievements);

        }
        return true;
    }
}*/
